/**
 * Project: MainClass
 * 
 * File Created at 2010-8-6
 * $Id$
 * 
 * Copyright 2008 Alibaba.com Croporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Alibaba Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Alibaba.com.
 */
package com.xershop.common;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;

public class FileTools {

    public static void saveCodeToFile(String code, String filePath, String charSet) {
        FileOutputStream fileoutputstream = null;
        OutputStreamWriter outputStreamWriter = null;
        try {
            File file = new File(filePath);
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            fileoutputstream = new FileOutputStream(filePath);
            outputStreamWriter = new OutputStreamWriter(fileoutputstream, charSet);
            char[] tag_bytes = code.toCharArray();
            outputStreamWriter.write(tag_bytes, 0, tag_bytes.length);
            outputStreamWriter.flush();
            outputStreamWriter.close();
            fileoutputstream.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != outputStreamWriter) {
                    outputStreamWriter.close();
                }
                if (null != fileoutputstream) {
                    fileoutputstream.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static String loadCodeFromFile(String filePath, String charSet) {
        StringBuffer result = new StringBuffer("");
        InputStreamReader in = null;
        BufferedReader reader = null;
        try {
            in = new InputStreamReader(new FileInputStream(filePath), charSet);
            reader = new BufferedReader(in);
            String code = "";
            while ((code = reader.readLine()) != null) {
                result.append(code).append("\n");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != reader) {
                    reader.close();
                }
                if (null != in) {
                    in.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return result.toString();
    }
    
    public static String loadCodeFromURL(String link, String charSet) {
        StringBuffer codeBuffer = new StringBuffer("");
        BufferedReader in = null;
        try {
            URLConnection uc = new URL(link).openConnection();
            uc.setConnectTimeout(5000);
            uc.setReadTimeout(10000);
            in = new BufferedReader(new InputStreamReader(uc.getInputStream(), charSet));
            String tempCode = "";
            while ((tempCode = in.readLine()) != null) {
                codeBuffer.append(tempCode).append("\n");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != in) {
                    in.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return codeBuffer.toString();
    }

    public static void deleteFile(String filePath) {
        try {
            File inputFile = new File(filePath);
            if (inputFile.isDirectory()) {
                File[] fl = inputFile.listFiles();
                for (int i = 0; i < fl.length; i++) {
                    fl[i].delete();
                }
            } else {
                inputFile.delete();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
